package com.rjt.ele.controller;

import blade.kit.http.HttpRequest;
import com.rjt.common.util.CookieUtil;
import com.rjt.common.util.JsonUtil;
import com.rjt.common.util.TextUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

/**
 * 
* @comment
* @author renjiangtao 
* @date 2018年10月16日  
* @version 1.0
 */
@Controller
@RequestMapping("ele")
public class EleController {
	private Logger logger = LoggerFactory.getLogger(EleController.class);

	private String cookie;

	private String validateToken;

	private String captchaHash;

	private String userId;

	@GetMapping("check")
	@ResponseBody
	public String check(String mobile) {
		try {
			String url = "http://api.osamedia.cn/webService/shopGatherController?m=elmNew";
			HttpRequest request = HttpRequest.post(url, true, "r_url", "", "mobile", mobile).header("Referer",
					"http://api.osamedia.cn/webService/shopGather/elm-new/index.html?channelId=32010370");
			String res = request.body();
			System.out.println(res);
			request.disconnect();
			logger.error("elm 新人红包");
			return res;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	@PostMapping("sendCode")
	@ResponseBody
	public String sendCode(String mobile, @RequestParam(defaultValue = "") String captchaValue,
			HttpServletRequest req) {
		String url = "https://h5.ele.me/restapi/eus/login/mobile_send_code";
		String ua = req.getHeader("User-Agent");
		HttpRequest request = HttpRequest
				.post(url, true, "mobile", mobile, "captcha_value", captchaValue, "captcha_hash", captchaHash)
				.header("User-Agent", ua);
		String res = request.body();
		request.disconnect();
		System.out.println(res);
		validateToken = JsonUtil.getJsonValue(res, "validate_token");
		if (validateToken != "") {
			return "success";
		} else {
			return JsonUtil.getJsonValue(res, "message");
		}
	}

	@PostMapping("captchas")
	@ResponseBody
	public String captchas(String mobile, HttpServletRequest req) {
		String url = "https://h5.ele.me/restapi/eus/v3/captchas";
		String ua = req.getHeader("User-Agent");
		HttpRequest request = HttpRequest.post(url, true, "captcha_str", mobile).header("User-Agent", ua);
		String res = request.body();
		request.disconnect();
		System.out.println(res);
		captchaHash = JsonUtil.getJsonValue(res, "captcha_hash");
		if (captchaHash != "") {
			return JsonUtil.getJsonValue(res, "captcha_image");
		} else {
			return JsonUtil.getJsonValue(res, "message");
		}
	}

	@PostMapping("login")
	@ResponseBody
	public String login(String mobile, String validateCode, HttpServletRequest req) {
		String url = "https://h5.ele.me/restapi/eus/login/login_by_mobile";
		String ua = req.getHeader("User-Agent");
		HttpRequest request = HttpRequest
				.post(url, true, "mobile", mobile, "validate_code", validateCode, "validate_token", validateToken)
				.header("User-Agent", ua);
		String res = request.body();
		this.cookie = CookieUtil.getCookie(request);
		System.out.println(cookie);
		System.out.println(res);
		userId = JsonUtil.getJsonValue(res, "user_id");
		request.disconnect();
		return res;
	}

	@GetMapping("ballots")
	@ResponseBody
	public String ballots(HttpServletRequest req) throws UnsupportedEncodingException {
		String ua = req.getHeader("User-Agent");
		String url="https://h5.ele.me/restapi/traffic/campagin/basketball/ballots";
		HttpRequest request = HttpRequest.post(url, true,"user_id",userId).header("Cookie", this.cookie).header("User-Agent", ua);
		String res = request.body();
		System.out.println(res);
		request.disconnect();
		return res;
	}
	@GetMapping("assistanceVote")
	@ResponseBody
	public String assistanceVote(HttpServletRequest req,String id) throws UnsupportedEncodingException {
		String ua = req.getHeader("User-Agent");
		String result = TextUtil.getWd(req);
		String lat = "37.874690";
		String lng = "112.537170";
		if (result.indexOf(",") != -1) {
			 lng = result.split(",")[0];
			 lat = result.split(",")[1];
		}
		String  url = "https://h5.ele.me/restapi/traffic/campagin/basketball/assistanceVote";
		HttpRequest request = HttpRequest.post(url, true,"inviter_ballot_id",id,
			"user_id",userId,"lat",lat,"lng",lng,"candidate_id",1,
			"send_red_packet","1","invitee_ballot_id",id,"invitee_id","",
			"inviter_id","").header("Cookie", this.cookie).header("User-Agent", ua);
		String res = request.body();
		System.out.println(res);
		request.disconnect();
		return res;
	}
	@GetMapping("supervip")
	@ResponseBody
	public String supervip(HttpServletRequest req) throws UnsupportedEncodingException {
		String ua = req.getHeader("User-Agent");
		String result = TextUtil.getWd(req);
		String lat = "37.874690";
		String lng = "112.537170";
		if (result.indexOf(",") != -1) {
			 lng = result.split(",")[0];
			 lat = result.split(",")[1];
		}
		String url="https://h5.ele.me/restapi/member/v1/users/" + userId +"/supervip/growth/prize";
		HttpRequest request = HttpRequest.post(url, true,"channel","taopiaopiao_banner_1",
				"latitude",lat,"longitude",lng).header("Cookie", this.cookie).header("User-Agent", ua);
		String res = request.body();
		StringBuffer sb=new StringBuffer();
		res=JsonUtil.getJsonValue(res, "header_text");
		sb.append(res+"|");
		url="https://h5.ele.me/restapi/member/v1/users/" + userId +"/supervip/growth/prize";
		request = HttpRequest.post(url, true,"channel","tongchen_order_detail_page_1",
				"latitude",lat,"longitude",lng).header("Cookie", this.cookie).header("User-Agent", ua);
		res = request.body();
		res=JsonUtil.getJsonValue(res, "header_text");
		sb.append(res+"|");
		url="https://h5.ele.me/restapi/member/v1/users/" + userId +"/supervip/growth/prize";
		request = HttpRequest.post(url, true,"channel","dingding_offline_1").header("Cookie", this.cookie)
				.header("User-Agent", ua);
		res = request.body();
		res=JsonUtil.getJsonValue(res, "header_text");
		sb.append(res);
		request.disconnect();
		return sb.toString();
	}
	@GetMapping("hongbao")
	@ResponseBody
	public String hongbao(HttpServletRequest req) {
		String url = "https://h5.ele.me/restapi/promotion/v3/users/" + userId + "/hongbaos?offset=0&limit=50";
		String ua = req.getHeader("User-Agent");
		HttpRequest request = HttpRequest.get(url, true).header("Cookie", this.cookie).header("User-Agent", ua);
		String res = request.body();
		request.disconnect();
		return res;
	}
	@GetMapping("index")
	public String ele(HttpServletRequest request) {
		logger.warn("ip:{} ele活动区", TextUtil.getRemortIP(request));
		return "ele";
	}
}
